package tongwentongshu.com.app.fragment;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;
import tongwentongshu.com.app.R;
import tongwentongshu.com.app.activity.ReadingDetailsActivity;
import tongwentongshu.com.app.adapter.MyReadingAdapter;
import tongwentongshu.com.app.bean.LoginEvent;
import tongwentongshu.com.app.bean.MyReadingBean;
import tongwentongshu.com.app.bean.ReadingEvent;
import tongwentongshu.com.app.db.Cache;
import tongwentongshu.com.app.dialog.AlertUtils;
import tongwentongshu.com.app.network.ResponseSubscriber;
import tongwentongshu.com.app.update.UpdateFractory;

/**
 * Created by XiongBin on 2017/7/12.
 */

public class MyReadingFragment extends BasePagerFragment implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {
    private Map<String, String> map;
    protected boolean onRefreshJudge = false;
    private MyReadingAdapter myReadingAdapter;

    public MyReadingFragment(Activity activity) {
        super(activity);
    }

    @Override
    public void initData() {
        super.initData();
        initList();
        initMyReading();
//        setEventBus();
    }

    public void onEventMainThread(ReadingEvent result) {
        initMyReading();
    }

    private void initList() {
        onRefreshJudge = true;
        mSwipeRefreshLayout.setOnRefreshListener(this);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
        List arr = new ArrayList<MyReadingBean.DataBean>();
        myReadingAdapter = new MyReadingAdapter(getActivity(), arr);
        myReadingAdapter.setOnLoadMoreListener(this, mRecyclerView);
        myReadingAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);

        mRecyclerView.setAdapter(myReadingAdapter);
        mRecyclerView.addOnItemTouchListener(new OnItemClickListener() {
            @Override
            public void onSimpleItemClick(final BaseQuickAdapter adapter, final View view, final int position) {
                Bundle b = new Bundle();
                b.putBoolean("pager_Judge", false);
                b.putString("pagerType", "1");
                b.putString("bookId", myreading.getData().get(position).getBookid());
                startActivity(ReadingDetailsActivity.class, b);
            }
        });
    }

//    protected void setEventBus() {
//        EventBus.getDefault().register(getActivity());
//    }

    /**
     * 携带数据页面跳转
     *
     * @param clz
     * @param bundle
     */
    public void startActivity(Class<?> clz, Bundle bundle) {
        Intent intent = new Intent();
        intent.setClass(getActivity(), clz);
        if (bundle != null) {
            intent.putExtras(bundle);
        }
        startActivity(intent);
    }

    void initMyReading() {
        AlertUtils.showLoding(mContext);
        map = new ArrayMap<>();
        map.put("token", Cache.getToken());
        map.put("pagesize", "50");
        if (!readingStatus.equals("0"))
            map.put("status", getStatus());

        UpdateFractory.getBuild()
                .name("MyReadingCall")
                .map(map)
                .build()
                .execute(new ResponseSubscriber<MyReadingBean>() {

                    @Override
                    public void onFailure(Throwable e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onSuccess(MyReadingBean myreading, String msg) {
                        chengeData(myreading);
                    }

                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        AlertUtils.disMissLoding();
                    }
                });
    }

    @Override
    public void onRefresh() {
        onRefreshJudge = true;
//        pullToRefreshAdapter.setEnableLoadMore(false);
        initMyReading();
    }

    @Override
    public void onLoadMoreRequested() {
        myReadingAdapter.loadMoreEnd(true);
    }

    MyReadingBean myreading;

    void chengeData(MyReadingBean myreading) {
        View empty = mActivity.getLayoutInflater().inflate(R.layout.empty_no_data, (ViewGroup) mRecyclerView.getParent(), false);
        //pullToRefreshAdapter.setPreLoadNumber(3);
        myReadingAdapter.setEmptyView(empty);
        this.myreading = myreading;
        if (myreading.getData() != null && myreading.getData().size() > 0) {
            if (onRefreshJudge) {
//                Log.e(TAG, "onRefreshJudge: " + onRefreshJudge);
                myReadingAdapter.setNewData(myreading.getData());
                mSwipeRefreshLayout.setRefreshing(false);
                myReadingAdapter.setEnableLoadMore(true);
                onRefreshJudge = false;
            }
        } else {
            mSwipeRefreshLayout.setRefreshing(false);
            myReadingAdapter.setEnableLoadMore(true);
            onRefreshJudge = false;
        }
    }
}
